<template>
  <el-card class="box-card" shadow="never">
    <div slot="header" class="clearfix">
      <span>编辑实体类</span>
    </div>
    <div>
      <el-form
        v-if="shiTiLRecordEdit"
        ref="shiTiLRecordEdit"
        :model="shiTiLRecordEdit"
        :rules="rules"
        label-width="100px"
        class="demo-mecAddData"
      >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="实体类名称:" prop="shiTiLMC">
              <el-input v-model="shiTiLRecordEdit.shiTiLMC" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实体类代号:" prop="shiTiLDH">
              <el-input v-model="shiTiLRecordEdit.shiTiLDH" />
            </el-form-item>
          </el-col>

          <!-- <el-col :span="8">
          <el-form-item label="表名:" prop="biaoMing">
            <el-input v-model="shiTiLRecordEdit.biaoMing"></el-input>
          </el-form-item>
          </el-col>-->
          <!--          <el-col :span="6">-->
          <!--            <el-form-item label="实体类类别:" prop="shitiLLB">-->
          <!--              <el-select-->
          <!--                v-if="shiTiLRecordEdit.shitiLLB"-->
          <!--                v-model="shiTiLRecordEdit.shitiLLB"-->
          <!--                value-key="record.id"-->
          <!--                placeholder="请选择"-->
          <!--              >-->
          <!--                <el-option-->
          <!--                  v-for="item in shitiLLB"-->
          <!--                  :key="item.value"-->
          <!--                  :label="item.name"-->
          <!--                  :value="item"-->
          <!--                />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="6">-->
          <!--            <el-form-item label="应用系统:" prop="yingYongXT">-->
          <!--              <el-select-->
          <!--                v-if="shiTiLRecordEdit.yingYongXT"-->
          <!--                v-model="shiTiLRecordEdit.yingYongXT"-->
          <!--                value-key="record.id"-->
          <!--                placeholder="请选择"-->
          <!--              >-->
          <!--                <el-option-->
          <!--                  v-for="item in yingYongXT"-->
          <!--                  :key="item.value"-->
          <!--                  :label="item.name"-->
          <!--                  :value="item"-->
          <!--                />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注:" prop="beiZhu">
              <el-input
                v-model="shiTiLRecordEdit.beiZhu"
                type="textarea"
                maxlength="200"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 6 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-link type="primary" @click="addRow">新增</el-link>

        <template>
          <el-table
            v-if="shiTiLRecordEdit.zds"
            border
            :data="shiTiLRecordEdit.zds"
            style="width: 100%;"
            :height="screenHeight - 410"
            size="small"
            :max-height="screenHeight"
            class="ziduan dept-content"
          >
            <el-table-column type="index" width="55" label="序号" fixed />
            <el-table-column label="名称">
              <template slot-scope="scope">
                <!-- <el-form-item :prop="'ziDuan.' + scope.$index + '.mingCheng'" :rules="rules.mingCheng"> -->
                <el-input v-model="scope.row.ziDuanMC" size="mini" />
                <!-- </el-form-item> -->
              </template>
            </el-table-column>
            <el-table-column prop="daiHao" label="代号" style="width: ;">
              <template slot-scope="scope">
                <!-- <el-form-item :prop="'ziDuan.' + scope.$index + '.daiHao'" :rules="rules.daiHao"> -->
                <el-input v-model="scope.row.ziDuanDH" size="mini" />
                <!-- </el-form-item> -->
              </template>
            </el-table-column>
            <!-- 暂不使用字段 -->
            <!-- <div >

                          <el-table-column prop="lieMing" label="列名">
                          <template slot-scope="scope">
                            <el-form-item :prop="'ziDuan.' + scope.$index + '.lieMing'" :rules="rules.lieMing">
                              <el-input size="mini" v-model="scope.row.lieMing"></el-input>
                            </el-form-item>
                          </template>
                          </el-table-column>
                          <el-table-column prop="kuanDu" label="宽度">
                          <template slot-scope="scope">
                            <el-form-item :prop="'ziDuan.' + scope.$index + '.kuanDu'" :rules="rules.kuanDu">
                              <el-input size="mini" v-model="scope.row.kuanDu"></el-input>
                            </el-form-item>
                          </template>
                          </el-table-column>
                          <el-table-column prop="jingDu" label="精度">
                          <template slot-scope="scope">
                            <el-form-item :prop="'ziDuan.' + scope.$index + '.jingDu'" :rules="rules.jingDu">
                              <el-input size="mini" v-model="scope.row.jingDu"></el-input>
                            </el-form-item>
                          </template>
                          </el-table-column>
                          <el-table-column prop="shiTiL" label="实体类">
                          <template slot-scope="scope">
                            <el-form-item :prop="'ziDuan.' + scope.$index + '.shiTiL'" :rules="rules.shiTiL">
                              <el-select v-model="scope.row.shiTiL" size="small" value-key="id" filterable placeholder="请选择">
                                <el-option v-for="item in shiTiL" :key="item.id" :label="item.name" :value="item"></el-option>
                              </el-select>
                            </el-form-item>
                          </template>
                          </el-table-column>
            </div>-->
            <el-table-column prop="ziDuanLX" label="字段类型">
              <template slot-scope="scope">
                <!-- <el-form-item :prop="'ziDuan.' + scope.$index + '.ziDuanLX'" :rules="rules.ziDuanLX"> -->
                <el-select
                  v-model="scope.row.ziDuanLX"
                  size="small"
                  value-key="record.id"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in ziDuanLX"
                    :key="item.id"
                    :label="item.name"
                    :value="item"
                  />
                </el-select>
                <!-- </el-form-item> -->
              </template>
            </el-table-column>

            <el-table-column prop="guanLianSTL" label="关联实体类" width="160px">
              <template slot-scope="scope">
                <!-- <el-form-item :prop="'ziDuan.' + scope.$index + '.guanLianSTL'" :rules="rules.guanLianSTL"> -->
                <el-select
                  v-model="scope.row.guanLianSTL"
                  size="small"
                  value-key="record.id"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in guanLianSTL"
                    :key="item.id"
                    :label="item.name"
                    :value="item"
                  />
                </el-select>
                <!-- </el-form-item> -->
              </template>
            </el-table-column>
            <!-- 暂不使用字段 -->
            <!-- <div>
                    <el-table-column prop="jiLianFS" label="级联方式" width="140px">
                    <template slot-scope="scope">
                      <el-form-item :prop="'ziDuan.' + scope.$index + '.jiLianFS'" :rules="rules.jiLianFS">
                        <el-select v-model="scope.row.jiLianFS" size="small" value-key="id" filterable placeholder="请选择">
                          <el-option v-for="item in jiLianFS" :key="item.id" :label="item.name" :value="item"></el-option>
                        </el-select>
                      </el-form-item>
                    </template>
                    </el-table-column>
                    <el-table-column prop="guanLianFLZD" label="关联父类字段" width="150px">
                    <template slot-scope="scope">
                      <el-form-item :prop="'ziDuan.' + scope.$index + '.guanLianFLZD'" :rules="rules.guanLianFLZD">
                        <el-select v-model="scope.row.guanLianFLZD" size="small" value-key="id" filterable placeholder="请选择">
                          <el-option v-for="item in guanLianFLZD" :key="item.id" :label="item.name" :value="item"></el-option>
                        </el-select>
                      </el-form-item>
                    </template>
                    </el-table-column>
                    <el-table-column prop="huanCunDX" label="缓存对象值" width="120px">
                    <template slot-scope="scope">
                      <el-form-item :prop="'ziDuan.' + scope.$index + '.huanCunDX'" :rules="rules.huanCunDX">
                        <el-select v-model="scope.row.huanCunDX" size="small" value-key="value" filterable placeholder="请选择">
                          <el-option v-for="item in huanCunDX" :key="item.id" :label="item.name" :value="item"></el-option>
                        </el-select>
                      </el-form-item>
                    </template>
                    </el-table-column>
            </div>-->
            <el-table-column fixed="right" label="操作" align="center" width="120">
              <template slot-scope="scope">
                <!-- <el-button  @click.native.prevent="deleteRow(scope.$index, infiledList)" size="small"> 移除 </el-button> -->
                <!-- <el-form-item> -->
                <el-link
                  type="danger"
                  @click.native.prevent="deleteRow(scope.$index, shiTiLRecordEdit.ziDuan)"
                >移除</el-link>
                <!-- <el-button type="warning" size="small" @click.native.prevent="deleteRow">删除</el-button> -->
                <!-- </el-form-item> -->
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="close">取消</el-button>
        </el-form-item>-->
      </el-form>
      <!-- <p>
        {{shiTiLRecordEdit.zds.records.length}}
      </p>-->
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>-->
        <el-button type="primary" @click="submitForm">保存</el-button>
        <!-- <el-button @click="resetForm">重置</el-button> -->
        <el-button @click="close">取消</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
// import Axios from 'axios'
import { LiYuDataSource } from 'liyu-ui'
let shiTiLeiDatasource
export default {
  components: {},
  props: {
    // recordId: {
    //   type: String,
    //   required: false
    // }
  },
  data() {
    return {
      shiTiLInfo: {},
      infiledList: [],

      shiTiL: [
        { name: '实体类一', id: 1 },
        { name: '实体类二', id: 2 }
      ],
      ziDuanLX: [
        { name: '字段类型一', id: 1 },
        { name: '字段类型二', id: 2 }
      ],
      guanLianSTL: [
        { name: '关联实体类一', id: 1 },
        { name: '关联实体类二', id: 2 }
      ],
      jiLianFS: [
        { name: '级联方式一', id: 1 },
        { name: '级联方式二', id: 2 }
      ],
      guanLianFLZD: [
        { name: '关联一', id: 1 },
        { name: '关联二', id: 2 }
      ],
      yingYongXT: [
        { name: '系统一', id: 1 },
        { name: '系统二', id: 2 }
      ],
      shitiLLB: [
        { name: '类别一', id: 1 },
        { name: '类别二', id: 2 }
      ],
      huanCunDX: [
        { name: '是', value: 1 },
        { name: '否', value: 2 }
      ],
      shiTiLeiShow: false,
      FromLoading: true,
      screenHeight: document.body.clientHeight,
      shiTiLRecordEdit: {},
      rules: {
        shiTiLDH: [{ required: true, message: '请输入', trigger: 'blur' }],
        shiTiLMC: [{ required: true, message: '请输入', trigger: 'blur' }],
        biaoMing: [{ required: true, message: '请输入', trigger: 'blur' }],
        shitiLLB: [{ required: true, message: '请选择', trigger: 'change' }],
        yingYongXT: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        beiZhu: [{ required: true, message: '请输入', trigger: 'blur' }],
        daiHao: [{ required: true, message: '请输入', trigger: 'blur' }],
        mingCheng: [{ required: true, message: '请输入', trigger: 'blur' }],
        lieMing: [{ required: true, message: '请输入', trigger: 'blur' }],
        kuanDu: [{ required: true, message: '请输入', trigger: 'blur' }],
        jingDu: [{ required: true, message: '请输入', trigger: 'blur' }],
        shiTiL: [{ required: true, message: '请选择', trigger: 'change' }],
        ziDuanLX: [{ required: true, message: '请选择', trigger: 'change' }],
        guanLianSTL: [{ required: true, message: '请选择', trigger: 'change' }],
        jiLianFS: [{ required: true, message: '请选择', trigger: 'change' }],
        guanLianFLZD: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        huanCunDX: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },
  watch: {
    screenHeight(val) {
      // 为了避免频繁触发resize函数导致页面卡顿，使用定时器
      if (!this.timer) {
        // 一旦监听到的screenWidth值改变，就将其重新赋给data里的screenWidth
        this.screenHeight = val
        this.timer = true
        const that = this
        console.log(val)
        setTimeout(function() {
          // 打印screenWidth变化的值
          that.timer = false
        }, 400)
      }
    }
  },
  mounted() {
    const id = this.$route.query._id
    this.handleInitRecord(id)
    const that = this
    window.onresize = () => {
      return (() => {
        window.screenHeight = document.body.clientHeight
        that.screenHeight = window.screenHeight
      })()
    }
  },
  created() {},

  methods: {
    handleInitRecord(_id) {
      const that = this
      shiTiLeiDatasource = new LiYuDataSource({
        shiTiLeiDH: 'DEV_ShiTiLei'
      })
      shiTiLeiDatasource.obtain(
        {
          id: _id
        },
        recordSet => {
          that.shiTiLRecordEdit = recordSet.getRecordByIndex(0)
        }
      )
    },
    deleteRow(index, rows) {
      // 删除改行
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          rows.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRow(zdsRecordSet, event) {
      this.shiTiLRecordEdit.zds.push({
        ziDuanDH: '121',
        ziDuanMC: '名称',
        lieMing: '',
        kuanDu: '',
        jingDu: '',
        shiTiL: '',
        ziDuanLX: { name: '' },
        guanLianSTL: { name: '' },
        jiLianFS: '',
        guanLianFLZD: '',
        huanCunDX: ''
      })
    },
    submitForm() {
      this.$refs.shiTiLRecordEdit.validate(valid => {
        if (valid) {
          shiTiLeiDatasource.submit()
        }
      })
    },
    close() {
      this.shiTiLeiShow = false
      this.shiTiLRecordEdit = {
        id: '',
        shiTiLDH: '',
        shiTiLMC: '',
        biaoMing: '',
        shitiLLB: '',
        yingYongXT: '',
        beiZhu: ''
      }
    },
    OpenShiTiLeiShow(data) {
      this.shiTiLeiShow = true
      this.shiTiLRecordEdit = data
    }
  }
}
</script>

<style scoped>
.el-card {
  border-top: none !important;
}
/*修改滚动条样式*/
div::-webkit-scrollbar {
  width: 10px;
  height: 10px;
  /**/
}
div::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
}
div::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
/*修改滚动条样式*/
.dept-content >>> .div::-webkit-scrollbar {
  width: 10px;
  height: 10px;
  /**/
}
.dept-content >>> .div::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
}
.dept-content >>> .div::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
.ziduan >>> .el-form-item__content {
  margin-left: 0px !important;
}
.dialog-footer {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
